package dao.Impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import dao.AttenceDao;
import dao.BaseDao;
import entity.Attence;

public class AttenceDaoImpl extends BaseDao implements AttenceDao{
	
	private Connection conn=null;
	private PreparedStatement pstmt=null;
	private ResultSet rs=null;

	@Override
	public List<Attence> list() {
		List<Attence> list=new ArrayList<Attence>();
		try {
			conn=super.getConnection();
			String sql="select * from attence order by chkDate desc";
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while (rs.next()) {
				Attence att=new Attence();
				att.setChkDate(rs.getString("chkDate"));
				att.setDept(rs.getString("dept"));
				att.setEmpName(rs.getString("empName"));
				att.setId(rs.getInt("id"));
				att.setStatus(rs.getString("status"));
				list.add(att);
				
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return list;
	}

	@Override
	public int add(Attence att) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="insert into attence(empName,dept,chkDate,status) values(?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, att.getEmpName());
			pstmt.setObject(2, att.getDept());
			pstmt.setObject(3, att.getChkDate());
			pstmt.setObject(4, att.getStatus());
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

	@Override
	public int dele(int id) {
		int result=0;
		try {
			conn=super.getConnection();
			String sql="delete from attence where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			result=pstmt.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}

}
